package kgc.crm.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import kgc.crm.annotation.UserPower;
import kgc.crm.annotation.VerificationToken;
import kgc.crm.enums.PowerEnum;
import kgc.crm.model.Role;
import kgc.crm.service.IRoleMenuService;
import kgc.crm.service.IRoleService;
import kgc.crm.service.IUserRoleService;
import kgc.crm.util.ResultUtil;
import kgc.crm.vo.RoleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("role")
@CrossOrigin
public class RoleController {
    @Autowired
    private IRoleService roleService;
    @Autowired
    private IUserRoleService userRoleService;
    @Autowired
    private IRoleMenuService roleMenuService;
    //查看全部角色
    @VerificationToken
    @UserPower(power = PowerEnum.INDEX)
    @GetMapping("list")
    public ResponseEntity list() {
        return roleService.list();
    }

    @VerificationToken
    @UserPower(power = PowerEnum.INDEX)
    @PostMapping("list/user")
    //根据id插叙该角色下所有用户 RoleVo vo 返回的类型
    public ResponseEntity ListByRoleId(@RequestBody RoleVo vo) {
        return roleService.listByRoleId(vo);
    }
    //添加角色
    @VerificationToken
    @UserPower(power = PowerEnum.SAVE)
    @PostMapping("add")
    public ResponseEntity add(@RequestBody  Role role) {
        return roleService.add(role);
    }
    //修改角色
    @VerificationToken
    @UserPower(power = PowerEnum.UPDATE)
    @PostMapping("upd")
    public ResponseEntity upd(@RequestBody  Role role) {
        return roleService.upd(role);
    }
    //删除角色
    @VerificationToken
    @UserPower(power = PowerEnum.DELETE)
    @PostMapping("del")
    public ResponseEntity del(@RequestBody  Role role) {
        return ResultUtil.success(roleService.del(role));
    }
    //复制角色
    @VerificationToken
    @UserPower(power = PowerEnum.SAVE)
    @PostMapping("copy")
    public ResponseEntity copy(@RequestBody Role role){
        return roleService.copy(role);
    }


    //关联员工
    @VerificationToken
    @UserPower(power = PowerEnum.SAVE)
    @PostMapping("associate/role/{id}")
    public ResponseEntity associate(@PathVariable("id") Integer roleId,@RequestBody String userIds){
        //JSONObject object = JSON.parseObject(ids);
        ResponseEntity entity = userRoleService.associate(roleId,userIds);
        return entity;
    }

    //角色权限
    @VerificationToken
    @UserPower(power = PowerEnum.SAVE)
    @PostMapping("rolePermissions/roleId/{id}")
    public ResponseEntity rolePermissions(@PathVariable("id") Integer roleId,@RequestBody String MenuIds){
        //JSONObject object = JSON.parseObject(ids);
        ResponseEntity entity = roleMenuService.rolePermissions(roleId,MenuIds);
        return entity;
    }
}
